package com.xy.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date022/4/9 11:49
 * @description
 */

@Component
@Aspect
public class LogAspects {

    @Pointcut("execution(* com.xy.aop.MathCalculate.*(..))")
    public void pointcut() {

    }

    @Around("pointcut()")
    public void log(ProceedingJoinPoint pj) {
        String name = pj.getSignature().getName();
        String args = Arrays.toString(pj.getArgs());
        try {
            System.out.println(name + "运行...@Before参数列表是：{" + args + "}");
            Object proceed = pj.proceed();
            System.out.println(name + "返回...@AfterReturning运行结果：{" + proceed + "} ");
        } catch (Throwable throwable) {
            System.err.println(name + "异常...@AfterThrowing异常信息：{" + throwable + "}");
        } finally {
            System.out.println(name + "结束...@After");
        }
    }

    //@Before("pointcut()")
    //public void logStart(JoinPoint joinPoint) {
    //    String name = joinPoint.getSignature().getName();
    //    String args = Arrays.toString(joinPoint.getArgs());
    //    System.out.println(name + "运行...@Before参数列表是：{" + args + "}");
    //}
    //
    //@After("pointcut()")
    //public void logEnd(JoinPoint joinPoint) {
    //    String name = joinPoint.getSignature().getName();
    //    System.out.println(name + "结束...@After");
    //}
    //
    //@AfterReturning(value = "pointcut()", returning = "result")
    //public void logReturn(JoinPoint joinPoint, Object result) {
    //    String name = joinPoint.getSignature().getName();
    //    System.out.println(name + "返回...@AfterReturning运行结果：{" + result + "} ");
    //}
    //
    //@AfterThrowing(value = "pointcut()", throwing = "exception")
    //public void logException(JoinPoint joinPoint, Exception exception) {
    //    String name = joinPoint.getSignature().getName();
    //    System.out.println(name + "异常...@AfterThrowing异常信息：{" + exception + "}");
    //}
}
